﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Configuration;
using System.Threading;
namespace AutoSendEmail
{
    partial class SendEmail : ServiceBase
    {
        public SendEmail()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。
        }

        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
        }

        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            #region old 逻辑
            //DateTime curDateTime = DateTime.Now;
            //string StartTime = ConfigurationSettings.AppSettings["StartTime"].ToString();
            //string tmpTime = curDateTime.Hour.ToString().PadLeft(2, '0') + ":" + curDateTime.Minute.ToString().PadLeft(2, '0');
            //string parafilename = System.AppDomain.CurrentDomain.BaseDirectory + @"log";
            //string fileName = parafilename + "\\Log" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
            //if (tmpTime.CompareTo(StartTime) >= 0)  //当前时间等于配置开始时间开始执行逻辑
            //{
            //    if (!File.Exists(fileName))
            //    {
            //        try
            //        {
            //            Log.WriteLog("开始执行");
            //            DataOperate op = new DataOperate();
            //            string looptime = op.Init();
            //            timer1.Interval = double.Parse(looptime) * 60 * 60 * 1000;
            //            op.exportExcel();
            //            Log.WriteLog("执行结束");

            //        }


            //        catch (Exception ex)
            //        {
            //            Log.WriteLog(ex.ToString());
            //        }

            //    }
            //}
            #endregion

            DateTime curDateTime = DateTime.Now;
            string parafilename = System.AppDomain.CurrentDomain.BaseDirectory + @"log";
            string fileName = parafilename + "\\Log" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
            try
            {
                Log.WriteLog("开始执行");
                DataOperate op = new DataOperate();
                string looptime = op.Init();
                timer1.Interval = double.Parse(looptime) * 60 * 60 * 1000;
                op.exportExcel();
                Log.WriteLog("执行结束");

            }
            catch (Exception ex)
            {
                Log.WriteLog(ex.ToString());
            }

        }
    }
}
